package com.component;

import com.Listener.ActionDoneListener;
import com.bean.Farmer;
import com.bean.ProductCategory;
import com.bean.Users;
import com.dao.FarmerDao;
import com.dao.ProductCategoryDao;
import com.dao.UserDao;
import com.util.ScreenUtils;
import com.util.SelectProductCategory;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AddProductCategoryDialog extends JDialog {
    final int WIDTH = 500;
    final int HEIGHT = 400;

    private ActionDoneListener listener;

    public AddProductCategoryDialog(JFrame jf, String title, boolean isModel,ActionDoneListener listener){
        super(jf,title,isModel);
        this.listener  = listener;
        //组装视图
        this.setBounds((ScreenUtils.getScreenWidth()-WIDTH)/2,(ScreenUtils.getScreenHeight()-HEIGHT)/2,WIDTH,HEIGHT);

        Box vBox = Box.createVerticalBox();

        //组装农产品类别编号
        Box IDBox = Box.createHorizontalBox();
        JLabel IDLabel = new JLabel("类别编号：");
        JTextField IDField = new JTextField(15);

        IDBox.add(IDLabel);
        IDBox.add(Box.createHorizontalStrut(15));
        IDBox.add(IDField);

        //组装类别名称
        Box nameBox = Box.createHorizontalBox();
        JLabel nameLabel = new JLabel("类别名称：");
        JTextField nameField = new JTextField(15);

        nameBox.add(nameLabel);
        nameBox.add(Box.createHorizontalStrut(15));
        nameBox.add(nameField);

        //组装备注
        Box remaksBox = Box.createHorizontalBox();
        JLabel remaksLabel = new JLabel(" 备       注 ：");
        JTextField remaksField = new JTextField(15);

        remaksBox.add(remaksLabel);
        remaksBox.add(Box.createHorizontalStrut(15));
        remaksBox.add(remaksField);


        //组装按钮
        Box btnBox = Box.createHorizontalBox();
        JButton addBtn = new JButton("添加");
        addBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //获取用户的录入
                int categoryID = Integer.parseInt(IDField.getText().trim());
                String categoryName = nameField.getText().trim();
                String Remarks = remaksField.getText().trim();

                if(IDField.getText().trim().length()!=4){
                    JOptionPane.showMessageDialog(jf, "类别编号要为四位数字！");
                }
                else if(!new SelectProductCategory().notexitProductCategoryID(categoryID)){
                    JOptionPane.showMessageDialog(jf,"该类别编号已存在！");
                }
                else{
                    //将数据添加进数据库
                    ProductCategory productCategory = new ProductCategory(categoryID,categoryName,Remarks);
                    new ProductCategoryDao().insertProductCategory(productCategory);
                    JOptionPane.showMessageDialog(jf, "添加成功!");
                    listener.done(null);
                    dispose(); // 关闭添加窗口
                }
            }
        });


        btnBox.add(addBtn);

        vBox.add(Box.createVerticalStrut(40));
        vBox.add(IDBox);
        vBox.add(Box.createVerticalStrut(30));
        vBox.add(nameBox);
        vBox.add(Box.createVerticalStrut(30));
        vBox.add(remaksBox);
        vBox.add(Box.createVerticalStrut(30));
        vBox.add(btnBox);

        //为了左右有间距，在vBox外层封装一个水平的Box，添加间隔
        Box hBox = Box.createHorizontalBox();
        hBox.add(Box.createHorizontalStrut(50));
        hBox.add(vBox);
        hBox.add(Box.createHorizontalStrut(50));

        this.add(hBox);
    }
}
